// pages/logins/logins.js
// 获取应用实例
const app = getApp()
import baseuser from '~/utils/users.js'
import basesup from '~/utils/supplier.js'
import baseteam from '~/utils/teamleader.js'
Page({
    data: {
        motto: '请先登录',
        userInfo: {},
        hasUserInfo: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo'),
        canIUseGetUserProfile: false,
        canIUseOpenData: false // 如需尝试获取用户信息可改为false
    },
    // 事件处理函数
    bindViewTap() {
        wx.navigateTo({
              url: '/logins/logs/logs'

        })
    },
    copydata(items) {
        wx.setClipboardData({
            data: items.currentTarget.dataset.token,
            success(res) {
                wx.showToast({
                    title: 'copy ok',
                    icon: 'success',
                    duration: 2000
                })
            
            }
        })
    },
    setgys() {
        let setgys = {
            accountState: 0,
            applyFor: "2",
            avatar: "https://thirdwx.qlogo.cn/mmopen/vi_32/,JwxAvaINYGPW1aOtL7vG2LY3WtGicWibv95BaibdTst9JWCRFzHk9eFxD0friaveDAUgc05AJSu1SaYLO5RTubIBibA/132",
            createTime: "2023-08-28 10:36:37",
            delFlag: 0,
            gender: 0,
            headId: "1697089800894529538",
            id: 1005,
            identity: 2,
            name: "马硕——供应商.",
            openId: "oDkz64lGmacAssRyTSf3cs73TznI",
            phone: null,
            storeId: "1697531239977734146",
            token: "wAUx9tDGbF6PPjAOj8oFtkDjqIhUIgHHe1WKOkCBdj2jtnII4gbio979wBYvOwTS",
            unionid: "oRQq61eljYlooKRivL9AM6GeRq8w",
            updateTime: "2023-09-11 16:36:37"
        }
        wx.setStorage({
            key: "usermsg",
            data: setgys,
            success(res) {
                wx.showToast({
                    title: 'copy ok',
                    icon: 'success',
                    duration: 2000
                })
            }
        })
    },
    settz() {
        let settz = {
            accountState: 0,
            applyFor: "2",
            avatar: "https://thirdwx.qlogo.cn/mmopen/vi_32/,JwxAvaINYGPW1aOtL7vG2LY3WtGicWibv95BaibdTst9JWCRFzHk9eFxD0friaveDAUgc05AJSu1SaYLO5RTubIBibA/132",
            createTime: "2023-08-28 10:36:37",
            delFlag: 0,
            gender: 0,
            headId: "1697089800894529538",
            id: 1005,
            identity: 1,
            name: "赵燕香——团长.",
            openId: "oDkz64lGmacAssRyTSf3cs73TznI",
            phone: null,
            storeId: "1697531239977734146",
            token: "NQZGKYWpkByzEUszzr0Gnsr2Dsup73sc79JQKGDfbac69Pxag7j6YkG4Wx1ZpioN",
            unionid: "oRQq61eljYlooKRivL9AM6GeRq8w",
            updateTime: "2023-09-11 16:36:37"
        }
        wx.setStorage({
            key: "usermsg",
            data: settz,
            success(res) {
                wx.showToast({
                    title: 'copy ok',
                    icon: 'success',
                    duration: 2000
                })
            }
        })
    },
    onLoad() {
        let that = this
        wx.getStorage({
            key: 'usermsg',
            success(res) {
                console.log(res)
                that.setData({
                    userInfo: res.data,
                    hasUserInfo: true,
                    motto: "登录成功"
                })
            }
        })
    },
    getUserProfile(e) {
        let that = this
        // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
        let logindata = {}
        wx.getUserProfile({
            desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: (userres) => {
                console.log(userres)
                logindata.encryptedData = userres.encryptedData
                logindata.iv = userres.iv
                wx.login({
                    async success(coderes) {
                        logindata.code = coderes.code
                        let logins = await baseuser.login(logindata)
                        if (logins.code != 200) {
                            wx.showToast({
                                title: '登录失败，请重试',
                                icon: 'error',
                                duration: 2000
                            })
                            return false
                        }
                        wx.setStorage({
                            key: "usermsg",
                            data: logins.data.item
                        })
                        that.setData({
                            userInfo: logins.data.item,
                            motto: "登录成功"
                        })
                        wx.showToast({
                            title: '登录成功，即将返回上一个页面',
                            icon: 'none',
                            duration: 2000
                        })
                        var pages = getCurrentPages();
                        var before = pages[pages.length - 2];
                        setTimeout(() => {
                            wx.navigateBack({
                                success: () => {
                                    // 执行前一页面的onLoad方法
                                    before.onLoad();
                                }
                            })
                        }, 500);
                    }
                })
                that.setData({
                    userInfo: userres.userInfo,
                    hasUserInfo: true
                })
            }
        })
    },
    getUserInfo(e) {
        // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
        console.log(e)
        this.setData({
            userInfo: e.detail.userInfo,
            hasUserInfo: true
        })
    }
})
